require "common/Define"
require "framework/BaseController"
require "FairyGUI"

BundleUsageMain = BaseController:New({mControllerName = "BundleUsageMain"});

function BundleUsageMain:Main()
	-- body
	BundleUsageMain:Awake();
end

function BundleUsageMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown);

	StartCoroutine( self.LoadUIPackage );
end

function BundleUsageMain:LoadUIPackage()
	local wholePath = Application.streamingAssetsPath;
	print("wholePath: ", wholePath);
	local wholePath2, nums = string.gsub(wholePath, "\\", "/");
	print(wholePath2);
	local url = wholePath2 .. "/fairygui-examples/bundleusage.ab";
	print(url);
	if (Application.platform ~= UnityEngine.RuntimePlatform.Android) then
		url = "file:///" .. url;
	end

	local www = WWW.New(url);
	Yield(www);
	
	if (www.error == "" or www.error == nil ) then
		local bundle = www.assetBundle;
		if (bundle ~= null) then
			print("Run Window->Build FairyGUI example Bundles first.");
			Yield(0);
		end

		UIPackage.AddPackage(bundle);

		local _mainView = UIPackage.CreateObject("BundleUsage", "Main").asCom;
		_mainView.fairyBatching = true;
		_mainView:SetSize(GRoot.inst.width, GRoot.inst.height);
		_mainView:AddRelation(GRoot.inst, RelationType.Size);

		GRoot.inst:AddChild(_mainView);
		_mainView:GetTransition("t0"):Play();
	else
		print(www.error);
	end
end

function BundleUsageMain:OnKeyDown(context)
	if (context.inputEvent.keyCode == KeyCode.Escape) then
		Application.Quit();
	end
end